﻿//custom binding to initialize a jQuery UI dialog
ko.bindingHandlers.jqDialog = {
    init: function (element, valueAccessor) {
        var options = ko.utils.unwrapObservable(valueAccessor()) || {};

        //handle disposal
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).dialog("destroy");
        });

        $(element).dialog(options);
    }
};
// Controls whether or not the text in a textbox is selected based on a model property
ko.bindingHandlers.selected = {
    init: function (elem, valueAccessor) {
        $(elem).blur(function () {
            var boundProperty = valueAccessor();
            if (ko.isWriteableObservable(boundProperty)) {
                boundProperty(false);
            }
        });
    },
    update: function (elem, valueAccessor) {
        var shouldBeSelected = ko.utils.unwrapObservable(valueAccessor());
        if (shouldBeSelected) {
            $(elem).select();
        }
    }
};
// Makes a textbox lose focus if you press "enter"
ko.bindingHandlers.blurOnEnter = {
    init: function (elem, valueAccessor) {
        $(elem).keypress(function (evt) {
            if (evt.keyCode === 13 /* enter */) {
                evt.preventDefault();
                $(elem).triggerHandler("change");
                $(elem).blur();
            }
        });
    }
};
//custom binding handler that opens/closes the dialog
ko.bindingHandlers.openDialog = {
    update: function (element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        if (value) {
            $(element).dialog("open");
            $(element).find('iframe').focus();
        } else {
            $(element).dialog("close");
        }
    }
}

//custom binding to initialize a jQuery UI button
ko.bindingHandlers.jqButton = {
    init: function (element, valueAccessor) {
        var options = ko.utils.unwrapObservable(valueAccessor()) || {};

        //handle disposal
        ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
            $(element).button("destroy");
        });

        $(element).button(options);
    }
};

//custom binding for Rich Text Area

ko.bindingHandlers.jqNEdit = {

    init: function (element, valueAccessor, aBA, vm) {
          var desc =  ko.utils.unwrapObservable(aBA().value) || {};
          var value = valueAccessor();
          var textAreaContentElement = $(element);
          textAreaContentElement.attr('id', 'note' + value);
          if (desc && desc.length > 0) {
              $(textAreaContentElement).html(desc);
          }
          else {
              $(textAreaContentElement).html('Note Description...');
          }
          //$(textAreaContentElement).bind('change', function () {
          //    aBA().value($(this).html());
          //})
          var area = new nicEditor({
              iconsPath: config.nicimg,
            buttonList: ['bold', 'italic', 'link', 'ul', 'ol' ]
          });
          var panel = textAreaContentElement.parent().next().find('.editorpanel');
          panel.attr('id', 'editorpanel' + value);
        area.setPanel('editorpanel'  + value);
        area.addInstance('note' + value);
        $(panel).children('.nicEdit-panelContain').removeAttr('style');

    },
    update: function (element, valueAccessor) {
    }
};



//custom binding for auto resize textarea
ko.bindingHandlers.jqAutoresize = {

    init: function (element, valueAccessor, aBA, vm) {
        //var options = ko.utils.unwrapObservable(valueAccessor()) || {};
        //$(element).autosize(options); 
        var options = ko.utils.unwrapObservable(aBA().value) || {};
        //ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
        //    $(element).data('autosize').remove();
        //}); 
        $(element).val(options);
        $(element).css('height', '1em');
        $(element).autosize();

     // .trigger('autosize');


    },
    update: function (element, valueAccessor, aBA, vm) {
       
}
};

// Simulates HTML5-style placeholders on older browsers
ko.bindingHandlers.placeholder = {
    init: function (elem, valueAccessor) {
        var placeholderText = ko.utils.unwrapObservable(valueAccessor()),
            input = $(elem);

        input.attr('placeholder', placeholderText);

        // For older browsers, manually implement placeholder behaviors
        if (!Modernizr.input.placeholder) {
            input.focus(function () {
                if (input.val() === placeholderText) {
                    input.val('');
                    input.removeClass('placeholder');
                }
            }).blur(function () {
                setTimeout(function () {
                    if (input.val() === '' || input.val() === placeholderText) {
                        input.addClass('placeholder');
                        input.val(placeholderText);
                    }
                }, 0);
            }).blur();

            input.parents('form').submit(function () {
                if (input.val() === placeholderText) {
                    input.val('');
                }
            });
        }
    }
};